﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: GeneticCode.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2013-01-25 3:25 PM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Serialization;
using System.Text;
using BOL.Models.Seq;

namespace BOL.Models.Taxonomy
{
    [DataContract]
    public struct GeneticCode : IIdentifiable<int>, INamed
    {
        #region Private variables

        private readonly int _id;
        private string _name;
        private readonly string _abbreviation;
        private readonly Dictionary<Codon, char?> _table;

        #endregion

        #region Public properties

        public int Id { get { return _id; } }
        string IIdentifiable.Id { get { return _id.ToString(CultureInfo.InvariantCulture); } } 
        public string Name { get { return _name; } set { _name = value; } }
        public string Abbreviation { get { return _abbreviation; } }
        public Dictionary<Codon, char?> Table { get { return _table; } }

        #endregion

        #region Constructor

        public GeneticCode(int id, string name, string abbreviation, Dictionary<Codon, char?> table)
        {
            _id = id;
            _name = name;
            _abbreviation = abbreviation;
            _table = table;
        }

        #endregion

        #region Public methods

        public char? Translate(Codon codon)
        {
            return _table.ContainsKey(codon) ? _table[codon] : null;
        }

        //public IEnumerable<char?> Translate(string sequence)
        //{
        //    int length = sequence.Length - 2;
        //    char[] read;
        //    for (int i = 0; i < length; i++)
        //    {
        //        read = sequence.ToCharArray(i, 3);
        //        if (_table.ContainsKey(

        //        yield return Translate(new Codon(sequence.Take(3).ToArray(), Codon.CodonType.;
        //    }
        //}

        #endregion

        #region Object overriden

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var key in _table.Keys)
                sb.AppendFormat("{0}\t{1}\n", key, _table[key]);

            return sb.ToString();
        }

        #endregion
    }
}
